package net.falsecam.reihenaufnahme;

import java.io.IOException;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;


public class TsFileUpload extends HttpServlet {
	private BlobstoreService blobstoreService = BlobstoreServiceFactory
			.getBlobstoreService();

	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
		BlobKey blobKey = blobs.get("tsfile");

		if (blobKey == null) {
			res.sendRedirect("/");
		} else {
			TsFile tsfile = new TsFile(req.getParameter("language"),
					blobKey.getKeyString());
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try {
				pm.makePersistent(tsfile);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				pm.close();
			}
			res.sendRedirect("/translation");
		}
	}
}
